<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline" label-position="left">

      <el-form-item label="用户名称">
        <el-input v-model="paymentQuery.userName" style="width: 150px" placeholder="用户名称"/>
      </el-form-item>

      <el-form-item label="产品名称">
        <el-input v-model="paymentQuery.productName" style="width: 150px" placeholder="产品名称"/>
      </el-form-item>

      <el-form-item label="时间范围">
        <el-date-picker
          v-model="paymentQuery.startTime"
          type="datetime"
          placeholder="起始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          style="width: 175px"
        />
      </el-form-item>~
      <el-form-item>
        <el-date-picker
          v-model="paymentQuery.endTime"
          type="datetime"
          placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          style="width: 175px"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column label="产品名称" align="center" >
        <template slot-scope="scope">
          <router-link :to="'/product/detail/' + scope.row.productId">
            {{ scope.row.productName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="买家名称" align="center" >
        <template slot-scope="scope">
          <router-link :to="'/user/detail/' + scope.row.buyerId">
            {{ scope.row.buyerRealName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="卖家名称" align="center" >
        <template slot-scope="scope">
          <router-link :to="'/user/detail/' + scope.row.sellerId">
            {{ scope.row.sellerRealName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="购买数量" align="center" >
        <template slot-scope="scope">
          {{ scope.row.quantity + scope.row.unit }}
        </template>
      </el-table-column>

      <el-table-column label="支付金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.amount + '元' }}
        </template>
      </el-table-column>

      <el-table-column label="交易时间" align="center" width="200px" prop="happenTime"/>

      <el-table-column label="操作" align="center" width="200px" >
        <template slot-scope="scope">
          <el-button :disabled="buttonDisabled" type="success" @click="judge(scope.row.id, '1')">通过</el-button>
          <el-button :disabled="buttonDisabled" type="danger" @click="judge(scope.row.id, '-1')">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>

import paymentApi from '@/api/trace/payment'
export default {
  data() {
    return {
      listLoading: false,
      buttonDisabled: false,
      userTypeOptions: [
        {
          value: 1,
          label: '菜农'
        }, {
          value: 2,
          label: '菜企'
        }, {
          value: 3,
          label: '菜商'
        }
      ],
      list: [],
      current: 1,
      limit: 5,
      total: 10,
      paymentQuery: {
        judgeFlag: '0',
        buyerType: '2'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(current = 1) {
      this.current = current
      paymentApi.getPageList(this.current, this.limit, this.paymentQuery)
        .then(response => {
          this.list = response.data.list
          this.total = response.data.total
        }).catch(response => {
          console.log('error')
        })
    },
    resetData() {
      this.paymentQuery = {
        judgeFlag: '0',
        buyerType: '2'
      }
      this.getList()
    },
    judge(id, judgeFlag) {
      this.buttonDisabled = true
      const data = {
        id: id,
        judgeFlag: judgeFlag
      }
      paymentApi.judge(data).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.buttonDisabled = false
        this.getList()
      }).catch(res => {
        this.$message({
          type: 'error',
          message: '操作失败!'
        })
      })
    }
  }
}
</script>
